from ebl_pattern_writer import point, shape, field

class idt(list):
    def __init__(self,wavelength,buswidth,fingerlength,fingerspacing,contactleadlength,contactpadsize,n_unit_cells,unit_cell_type='standard'):
        # note, all dimensions are specified in microns, then multiplied by 100
        # at the end to get them into nanometers as required
        self.unit_cell_type = unit_cell_type
        self.n_unit_cells   = n_unit_cells
        self.contactpadsize = contactpadsize
        self.contactleadlength = contactleadlength
        self.wavelength     = wavelength
        self.fingerlength   = fingerlength
        self.fingerspacing   = fingerspacing
        self.width          = fingerlength+fingerspacing
        self.buswidth       = buswidth
        self.buswidth       = buswidth

        F = self.fingerlength
        L = self.wavelength
        
        self.draw_idts()
    
    def draw_idts(self):
        B = self.buswidth
        #draw first idt
        coords=[point(0,0)]
        for i in range(self.n_unit_cells):
            origin=coords[len(coords)-1]
            if i==0:
                origin=origin+point(0,-B)
            newcoords = self.get_unit_cell(origin,False)
            for c in newcoords:
                coords.append(point(c).translate(origin))
        newcoords = [[-(self.contactleadlength+self.n_unit_cells*self.wavelength),-B],
                     [-(self.contactleadlength+self.n_unit_cells*self.wavelength),0],
                     [-(self.n_unit_cells*self.wavelength),0],
                     [-(self.n_unit_cells*self.wavelength),0]]
        for c in newcoords:
            coords.append(point(c))
                
        for i in range(len(coords)):
            coords[i]=(coords[i].translate(0,self.width/2+B))*1000
        self.append(shape(coords))


        #draw second idt
        coords=[point(0,0)]
        for i in range(self.n_unit_cells):
            origin=coords[len(coords)-1]
            if i==0:
                origin=origin+point(0,B)
                newcoords = self.get_unit_cell(origin,True)
            for c in newcoords:
                coords.append(point(c).translate(origin))
        newcoords = [[-(self.contactleadlength+self.n_unit_cells*self.wavelength),B],
                     [-(self.contactleadlength+self.n_unit_cells*self.wavelength),0],
                     [-(self.n_unit_cells*self.wavelength),0],
                     [-(self.n_unit_cells*self.wavelength),0]] 
        for c in newcoords:
            coords.append(point(c))
            
        for i in range(len(coords)):
            coords[i]=(coords[i].translate(0,-(self.width/2+B)))*1000
        self.append(shape(coords))

    def get_unit_cell(self,origin,lower):
        coords=[]
        F = self.fingerlength
        L = self.wavelength
        B = self.buswidth
        if self.unit_cell_type=='standard':
            if lower:
                coords = [[0,        0],
                          [-L/2.0,   0],
                          [-L/2.0,   F],
                          [-3*L/4.0, F],
                          [-3*L/4.0, 0],
                          [-L,       0]]
            else:
                coords = [[0,      -F],
                          [-L/4.0, -F],
                          [-L/4.0,  0],
                          [-L,      0]]
        elif self.unit_cell_type=='double':
            if lower:
                coords = [[0,        0],
                          [-4*L/8.0, 0],
                          [-4*L/8.0, F],
                          [-5*L/8.0, F],
                          [-5*L/8.0, 0],
                          [-6*L/8.0, 0],
                          [-6*L/8.0, F],
                          [-7*L/8.0, F],
                          [-7*L/8.0, 0],
                          [-L,       0]]
            else:
                coords = [[0,        -F],
                          [-L/8.0,   -F],
                          [-L/8.0,    0],
                          [-2*L/8.0,  0],
                          [-2*L/8.0, -F],
                          [-3*L/8.0, -F],
                          [-3*L/8.0, -0],
                          [-L,       -0]]
        return coords
    def flipx(self):
        for sh in self:
            sh.flipx()
        return self
    def translate(self,*args):
        for sh in self:
            sh.translate(*args)
        return self

class idt_field(field):
    def __init__(self,fieldsize,wavelength,buswidth,fingerlength,fingerspacing,contactleadlength,contactpadsize,n_unit_cells,unit_cell_type='standard'):
        field.__init__(self,fieldsize)
        fieldsize=fieldsize*1000 # since field.__init__ does this to self.fieldsize, I want them to agree to avoid confusion
        
        idtset1 = idt(wavelength,buswidth,fingerlength,fingerspacing,contactleadlength,contactpadsize,n_unit_cells,'double')
        #idtset2 = idt(wavelength,buswidth,fingerlength,fingerspacing,contactleadlength,contactpadsize,n_unit_cells,'double').flipx().translate(point(2000000,0))

        self.shapelists.append(idtset1)
        #self.shapelists.append(idtset2)

for wavelength in [1, 2, 4, 8]:
    myfield=idt_field(600,wavelength,5,max([70,30*wavelength]),max([10,30.0*wavelength/7]),50,20,30,'double')
    myfield.writexmldom('field_lambda_'+str(wavelength)+'um')
    
print 'done'